package com.todaynews.home.adapter;

import android.content.Context;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import com.todaynews.R;
import com.todaynews.home.NewsListFragment;
import com.todaynews.model.Channel;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 频道的adapter
 */

public class ChannelPagerAdapter extends FragmentStatePagerAdapter {

    private List<NewsListFragment> fragments = new ArrayList<>();
    private List<Channel> channels;
    private Context context;
    private String[] channelCodes;

    public ChannelPagerAdapter(Context context,List<Channel> channelList, FragmentManager fm) {
        super(fm);
        channels = channelList != null ? channelList : new ArrayList<>();
        this.context = context;
        channelCodes = context.getResources().getStringArray(R.array.channel_code);
    }

    public ChannelPagerAdapter(List<NewsListFragment> fragmentList, List<Channel> channelList, FragmentManager fm) {
        super(fm);
        fragments = fragmentList != null ? fragmentList : new ArrayList<>();
        channels = channelList != null ? channelList : new ArrayList<>();
    }

    @Override
    public Fragment getItem(int position) {
        /*if(!(fragments.size() !=0 &&  fragments.size()>position)){
            Channel channel = channels.get(position);
            NewsListFragment newsFragment = new NewsListFragment();
            Bundle bundle = new Bundle();
            bundle.putString(Constant.CHANNEL_CODE, channel.channelCode);
            //是否是视频列表页面,根据判断频道号是否是视频
            bundle.putBoolean(Constant.IS_VIDEO_LIST, channel.channelCode.equals(channelCodes[1]));
            newsFragment.setArguments(bundle);
            fragments.add(newsFragment);//添加到集合中
        }*/
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return channels.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return channels.get(position).title;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}